package games.ninJumpDx.ui
{
	import core.Global;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.sensors.Accelerometer;
	
	import games.ninJumpDx.data.Animations;
	import games.ninJumpDx.data.GameData;
	import games.ninJumpDx.data.UIConfig;
	import games.ninJumpDx.tools.Tools;
	import games.ninJumpDx.ui.actions.Building;
	import games.ninJumpDx.ui.actions.MoveUnit;
	import games.ninJumpDx.ui.actions.SparkParticles;
	import utils.ActionFactory;
	import games.ninJumpDx.ui.factorys.ArrowFactory;
	import games.ninJumpDx.ui.factorys.BuildingFactory;
	import games.ninJumpDx.ui.factorys.DragonFactory;
	import games.ninJumpDx.ui.factorys.EnemyFactory;
	import games.ninJumpDx.ui.factorys.FgFactory;
	import games.ninJumpDx.ui.factorys.GargoyleFactory;
	import games.ninJumpDx.ui.factorys.MunitFactory;
	import games.ninJumpDx.ui.factorys.NinjaFactory;
	import games.ninJumpDx.ui.factorys.ScoreFactory;
	import games.ninJumpDx.ui.factorys.SparkParticlesFactory;
	import games.ninJumpDx.ui.factorys.TrollFactory;
	import games.ninJumpDx.ui.factorys.WallFactory;
	import games.ninJumpDx.ui.factorys.WeaponFactory;
	
	import utils.Cartoon;
	import utils.CountManager;
	
	public class GameContent extends Sprite
	{
		private var m_data:GameData;
		private var m_bg:Sprite;			// 背景
		private var m_distantView:Sprite;	// 远景层
		private var m_behind:Sprite;		// 墙后面的建筑层
		private var m_wall:Sprite;			// 墙的层
		private var m_ahead:Sprite;			// 墙前面的层  敌人建筑 or 敌人出现
		private var m_player:Sprite;		// 忍者所在层
		private var m_fg:Sprite;			// 前景层
		
		private var m_actionCreater:ActionFactory;
		private var m_timer:CountManager;
		private var m_time:Number;
		
		private var m_count:Number;		// 上一次生成敌人间隔

		public function GameContent(data:GameData)
		{
			super();
			m_data = data;
		}
		
		public function destroy():void
		{
			while(numChildren > 0)
			{
				removeChildAt(0);
			}
		}
		
		public function start():void
		{
			// 销毁所有
			destroy();
			// 重新初始化
			init();
			// 生成计时器
			m_time = 0;
			m_timer = new CountManager(this);
			m_timer.interval = 500;
			m_timer.addEventListener(Event.CHANGE,onTimerChange);
				
			Global.addAction(m_timer);
			// 生成背景
			m_bg.addChild(Tools.getBitmapByName("background",GFrame.MAIN_UI));
			// 前景
			m_actionCreater = new FgFactory();
			m_fg.addChild(m_actionCreater.makeAction("foreground"));
			m_actionCreater = new ScoreFactory();
			m_fg.addChild(m_actionCreater.makeAction());
			// 忍者
			m_actionCreater = new NinjaFactory();
			m_player.addChild(m_actionCreater.makeAction());
			// 墙
			m_actionCreater = new WallFactory();
			m_wall.addChild(m_actionCreater.makeAction("leftWall"));
			var wall:Sprite = m_actionCreater.makeAction("rightWall");
			wall.scaleX = -1;
			wall.x = UIConfig.getGameWidth();
			m_wall.addChild(wall);
			/*火花效果*/
			m_actionCreater = new SparkParticlesFactory();
			var sp:SparkParticles = SparkParticles(m_actionCreater.makeAction("spark"));
			sp.target = m_player.getChildAt(0);
			m_fg.addChild(sp);
			// 生成建筑-----------------test
			// bridge,chain,drawbridge
			//m_actionCreater = new BuildingFactory();
			//m_behind.addChild(m_actionCreater.makeAction("bridge"));
			
			//m_behind.addChild(m_actionCreater.makeAction("chain"));
			
			//m_behind.addChild(m_actionCreater.makeAction("drawbridge"));
		}
		
		private function init():void
		{
			m_bg = new Sprite();
			m_distantView = new Sprite();
			m_behind = new Sprite();
			m_wall = new Sprite();
			m_ahead = new Sprite();
			m_player = new Sprite();
			m_fg =new Sprite();
			
			addChild(m_bg);
			addChild(m_distantView);
			addChild(m_behind);
			addChild(m_wall);
			addChild(m_ahead);
			addChild(m_player);
			addChild(m_fg);
		}
		
		public function makeAction(name:String,target:DisplayObjectContainer = null):void
		{
			switch(name)
			{
				case "chain":
				case "bridge":
				case "drawbridge":
				case "drawbridge_reversion":
				case "archer1":
				case "archer1_reversion":
				case "catapult1":
				case "catapult1_reversion":
					m_actionCreater = new BuildingFactory();
					break;
				case "knight":
				case "knight_reversion":
				case "horse":
				case "horse_reversion":
					m_actionCreater = new EnemyFactory();
					break;
				case "arrow":
				case "arrow_reversion":
				case "boulder":
				case "boulder_reversion":
					m_actionCreater = new WeaponFactory();
					break;
				case "gargoyle_frozen":
				case "gargoyle_frozen_reversion":
					m_actionCreater = new GargoyleFactory();
					break;
				case "dragon":
				case "dragon_wing":
				case "dragon_reversion":
				case "dragon_wing_reversion":
					m_actionCreater = new DragonFactory();
					break;
				case "troll":
					m_actionCreater = new TrollFactory();
					break;
					
			}
			if(target == null) target = m_ahead;
			target.addChild(m_actionCreater.makeAction(name));
			
		}
		
		private function onTimerChange(e:Event):void
		{
			m_data.score += (m_timer.count - m_time) * m_data.speed ;
			m_time = m_timer.count;
			
			var temp:Number = Math.floor(m_data.score / 50);
			if(m_count != temp)
			{
				// ------------------生成敌人------------------
				var random:Number = Math.floor(Math.random() * 100);
				if(random < 10)
				{
					makeAction("chain",m_behind);
				}
				 /*
				else if(random < 20)
				{
					makeAction("bridge",m_behind);
					makeAction("troll");
				}else if(random < 30)
				{
					makeAction("drawbridge");
				}else if(random < 40)
				{
					makeAction("drawbridge_reversion");
				}
				else if(random < 50)
				{
					makeAction("knight");
				}
				else if(random < 55)
				{
					makeAction("knight_reversion");
				}
				
				else if(random < 60)
				{
					makeAction("archer1");					
					makeAction("arrow");
				}
				else if(random < 65)
				{
					makeAction("archer1_reversion");
					makeAction("arrow_reversion");
				}
				
				else if(random < 70)
				{
					makeAction("catapult1");
					makeAction("boulder");
				}
				else if(random < 75)
				{
					makeAction("catapult1_reversion");
					makeAction("boulder_reversion");
				}
				else if(random < 80)
				{
					makeAction("horse");
				}
				else if(random < 85)
				{
					makeAction("horse_reversion");
				} 
				 */
				else if(random < 85)
				{
					makeAction("gargoyle_frozen");
				}else if(random <90)
				{
					makeAction("gargoyle_frozen_reversion");
				}
				
				else if(random < 95)
				{
					makeAction("dragon");
					makeAction("dragon_wing");
					
				}else if(random <100)
				{					
					makeAction("dragon_reversion");
					makeAction("dragon_wing_reversion");
				} 
				// --------------生成结束-----------------
				m_count = temp;
			}
		}
	}
}